package com.easy.carpanel.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author Sunshine
 * @Description Intent相关工具类
 */
public class Utils {

    private Utils() {
        throw new UnsupportedOperationException("u can't fuck me...");
    }

//    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
//        ArrayList result = new ArrayList();
//        result.ad.addAll(bt1);
//        result.addAll(bt2);
//        return result.toArray();
//    }

    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
        byte[] bt3 = new byte[bt1.length+bt2.length];
        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
        return bt3;
    }

//    /**
//     * 生成简单二维码
//     *
//     * @param content                字符串内容
//     * @param width                  二维码宽度
//     * @param height                 二维码高度
//     * @param character_set          编码方式（一般使用UTF-8）
//     * @param error_correction_level 容错率 L：7% M：15% Q：25% H：35%
//     * @param margin                 空白边距（二维码与边框的空白区域）
//     * @param color_black            黑色色块
//     * @param color_white            白色色块
//     * @return BitMap
//     */
//    public static Bitmap createQRCodeBitmap(String content, int width, int height, String character_set, String error_correction_level, String margin, int color_black, int color_white) {
//        // 字符串内容判空
//        if (TextUtils.isEmpty(content)) {
//            return null;
//        }
//        // 宽和高>=0
//        if (width < 0 || height < 0) {
//            return null;
//        }
//        try {
//            /** 1.设置二维码相关配置 */
//            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
//            // 字符转码格式设置
//            if (!TextUtils.isEmpty(character_set)) {
//                hints.put(EncodeHintType.CHARACTER_SET, character_set);
//            }
//            // 容错率设置
//            if (!TextUtils.isEmpty(error_correction_level)) {
//                hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level);
//            }
//            // 空白边距设置
//            if (!TextUtils.isEmpty(margin)) {
//                hints.put(EncodeHintType.MARGIN, margin);
//            }
//            /** 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象 */
//            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//
//            /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
//            int[] pixels = new int[width * height];
//            for (int y = 0; y < height; y++) {
//                for (int x = 0; x < width; x++) {
//                    //bitMatrix.get(x,y)方法返回true是黑色色块，false是白色色块
//                    if (bitMatrix.get(x, y)) {
//                        pixels[y * width + x] = color_black;//黑色色块像素设置
//                    } else {
//                        pixels[y * width + x] = color_white;// 白色色块像素设置
//                    }
//                }
//            }
//            /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,并返回Bitmap对象 */
//            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
//            return bitmap;
//        } catch (WriterException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    //16进制字符串转byte[]
    public static byte[] hexStringToByteArray(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        Log.e("StringToByte===1", bytes+"==="+bytes[0]);

        return bytes;
    }

    //byte[]转16进制字符串
    public static String bytesToString(byte[] bytes) {
        final char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0F];

            sb.append(hexChars[i * 2]);
            sb.append(hexChars[i * 2 + 1]);
            sb.append(' ');
        }
        return sb.toString();
    }

    public static Bitmap stringtoBitmap(String string){
        //将字符串转换成Bitmap类型
        Bitmap bitmap=null;
        try {
            byte[]bitmapArray;
            bitmapArray= Base64.decode(string, Base64.DEFAULT);
            bitmap= BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }



    public static String bitmaptoString(Bitmap bitmap){
        //将Bitmap转换成字符串
        String string=null;
        ByteArrayOutputStream bStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,bStream);
        byte[]bytes=bStream.toByteArray();
        string= Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

    // 压缩
    public static String compress(String str) throws IOException {
        try {
            if (str == null || str.length() == 0) {
                return str;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            gzip.close();
            return out.toString("ISO-8859-1");

        } catch (Exception e) {
            Log.e("utils==e", e+"===");
            e.printStackTrace();

            return "";
        }

//        return out.toString("UTF-8");
//        return out.toString("GBK");
    }

    // 解压缩
    public static String decompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
//        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
//        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("GBK"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
        return out.toString();
    }

//    public static final byte[] compress(String paramString) {
//        if (paramString == null)
//            return null;
//        ByteArrayOutputStream byteArrayOutputStream = null;
//        ZipOutputStream zipOutputStream = null;
//        byte[] arrayOfByte;
//        try {
//            byteArrayOutputStream = new ByteArrayOutputStream();
//            zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
//            zipOutputStream.putNextEntry(new ZipEntry("0"));
//            zipOutputStream.write(paramString.getBytes());
//            zipOutputStream.closeEntry();
//            arrayOfByte = byteArrayOutputStream.toByteArray();
//        } catch (IOException localIOException5) {
//            arrayOfByte = null;
//        } finally {
//            if (zipOutputStream != null)
//                try {
//                    zipOutputStream.close();
//                } catch (IOException localIOException6) {
//                }
//            if (byteArrayOutputStream != null)
//                try {
//                    byteArrayOutputStream.close();
//                } catch (IOException localIOException7) {
//                }
//        }
//        return arrayOfByte;
//    }
//
//    @SuppressWarnings("unused")
//    public static final String decompress(byte[] paramArrayOfByte) {
//        if (paramArrayOfByte == null)
//            return null;
//        ByteArrayOutputStream byteArrayOutputStream = null;
//        ByteArrayInputStream byteArrayInputStream = null;
//        ZipInputStream zipInputStream = null;
//        String str;
//        try {
//            byteArrayOutputStream = new ByteArrayOutputStream();
//            byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
//            zipInputStream = new ZipInputStream(byteArrayInputStream);
//            ZipEntry localZipEntry = zipInputStream.getNextEntry();
//            byte[] arrayOfByte = new byte[1024];
//            int i = -1;
//            while ((i = zipInputStream.read(arrayOfByte)) != -1)
//                byteArrayOutputStream.write(arrayOfByte, 0, i);
//            str = byteArrayOutputStream.toString();
//        } catch (IOException localIOException7) {
//            str = null;
//        } finally {
//            if (zipInputStream != null)
//                try {
//                    zipInputStream.close();
//                } catch (IOException localIOException8) {
//                }
//            if (byteArrayInputStream != null)
//                try {
//                    byteArrayInputStream.close();
//                } catch (IOException localIOException9) {
//                }
//            if (byteArrayOutputStream != null)
//                try {
//                    byteArrayOutputStream.close();
//                } catch (IOException localIOException10) {
//                }
//        }
//        return str;
//    }

}
